package com.code516.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.code516.entity.CommentEntity;
import com.code516.entity.StudentEntity;
import com.code516.entity.WorkEntity;
import com.code516.entity.pojo.WorkStudentPojo;
import com.code516.service.CommentService;
import com.code516.service.WorkService;

@RequestMapping("workAction")
@Controller
public class WorkAction {

	@Resource
	private WorkService workService;
	@Resource
	private CommentService commentService;


	/**
	 * 社区首页，加载所有作品
	 * @return
	 */
	@RequestMapping("workLists")
	@ResponseBody
	public Map<String, Object> workLists(){
		Map<String, Object> map = new HashMap<String, Object>();
		List<WorkStudentPojo> workStudentPojos = workService.findAll();
		map.put("workStudentPojos", workStudentPojos);
		return map;
	}



	@RequestMapping("saveFile")
	@ResponseBody
	public Map<String, Object> saveFile(WorkEntity workEntity, HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		StudentEntity studentEntity = (StudentEntity) session.getAttribute("studentEntity");
		int result = workService.saveWorkEntity(workEntity, studentEntity.getStuId());
		map.put("flag", result);
		return map;
	}




	/**
	 * 我的社区，加载登录用户的所有作品
	 * @return
	 */
	@RequestMapping("loadWork")
	@ResponseBody
	public Map<String, Object> loadWork(HttpSession sessoin){
		Map<String, Object> map = new HashMap<String, Object>();
		StudentEntity studentEntity = (StudentEntity) sessoin.getAttribute("studentEntity");
		List<WorkStudentPojo> workStudentPojos = null;
		int total = 0;
		if(studentEntity != null){
			workStudentPojos = workService.findByStuId(studentEntity.getStuId());
			//加载个人信息
			total = workService.findTotal(studentEntity.getStuId());
			map.put("studentEntity", studentEntity);
		}
		map.put("total", total);
		map.put("workStudentPojos", workStudentPojos);
		return map;
	}


	/**
	 * 根据Id加载一个作品，加载对应作品，数据
	 * @param sessoin
	 * @return
	 */
	@RequestMapping("loadWorkById")
	@ResponseBody
	public Map<String, Object> loadWorkById(String workId, HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		WorkStudentPojo workEntity = workService.findById(workId);
		map.put("workEntity", workEntity);
		//返回该作品手机端URL
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		basePath += "main/mPlayer.html";
		map.put("id", workId);
		map.put("url", basePath);//http://192.169.1.104:8080/ide/build/mPlayer.html
		//加载该作品的所有评论
		List<CommentEntity> commentEntitys = commentService.findAllCommentByWorkId(workId);
		map.put("commentEntitys", commentEntitys);
		return map;
	}




}
